<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>

		<view class="kw_name_box" v-for="(item,index) in searchResults" :key="index" v-if="searchResults.length!==0">
			<view class="kw_name">
				<text class="kw_name_text" @click="getSearchResults(item.goods_id)">{{item.goods_name}}</text>
				<uni-icons type="forward"></uni-icons>
			</view>
		</view>
		<view class="search-title" v-if="searchResults.length===0">
			<view>搜索记录</view>
			<uni-icons type="trash-filled" size="20" @click="cleanSearchRecords"></uni-icons>
		</view>
		<block v-if="searchResults.length===0">
			<view class="searchRecords-box">
				<view class="searchRecords-item" v-for="(item,index) in searchRecords" :key="index">
					<text class="searchRecords-text" @click="getGoodsList(item)">{{item}}</text>
					<uni-icons type="closeempty" size="15" @click="deleteSearchRecordsItem(index)"></uni-icons>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延迟器的timeId
				timer: null,
				kw: '', //搜索关键字
				searchResults: [], //搜索结果
				searchRecords: [], //  搜索记录

			}
		},
		onLoad() {
			this.searchRecords = JSON.parse(uni.getStorageSync('kw') || '[]')

		},
		methods: {
			input(e) {
				//清楚延时器
				clearTimeout(this.timer)
				//重新启动一个延时器，并把timerId赋值给this.timer
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				//关键字是否为空
				if (this.kw === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				this.getsearchRecords()
			},
			getsearchRecords() {
				const set = new Set(this.searchRecords)
				// set.delete(this.kw)
				set.add(this.kw)
				this.searchRecords = Array.from(set)
				if (this.searchRecords[0] !== this.kw) {
					this.searchRecords.reverse()
				}
				uni.setStorageSync('kw', JSON.stringify(this.searchRecords))
			},
			////全部删除搜索记录
			cleanSearchRecords() {
				let _that = this
				// this.searchRecords = []
				uni.showModal({
					content: '是否确认全部删除',
					success: function(res) {
						if (res.confirm) {
							if (_that.searchRecords instanceof Array) {
								_that.searchRecords = []
								uni.removeStorageSync('kw')
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}

				})

			},
			//单个删除搜索记录
			deleteSearchRecordsItem(index) {
				// console.log(index);
				let _that = this
				uni.showModal({
					content: '是否确认删除',
					success: function(res) {
						if (res.confirm) {
							_that.searchRecords.findIndex((item, id) => {
								// console.log(id);
								if (id === index) {
									_that.searchRecords.splice(id, 1)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getGoodsList(item) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?query=${item}`
				})
			},
			getSearchResults(goods_id) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${goods_id}`
				})
			}


		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.search-title {
		width: 100%;
		display: flex;
		justify-content: space-between;
		margin: 5px 3px;
		height: 30px;
		line-height: 30px;
		border-bottom: 1px solid darkgray;
	}

	.kw_name {
		padding: 10px;
		border-bottom: 1px solid #ccc;

		.kw_name_text {
			width: 95%;
			text-overflow: ellipsis;
			overflow: hidden;
			white-space: nowrap;
			display: inline-block;
			vertical-align: top;
		}
	}

	.searchRecords-box {
		display: flex;
		flex-wrap: wrap;


		.searchRecords-item {
			background-color: #ccc;
			height: 20px;
			padding: 5px;
			border-radius: 8px;
			margin: 5px;

			.searchRecords-text {
				width: 50px;
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
				display: inline-block;
				vertical-align: top;
			}
		}

	}
</style>